package cn.com.xf.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONObject;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.context.ServletContextAware;

import cn.com.xf.web.security.UserInfo;

public abstract class AbstractBaseController implements ServletContextAware {
	protected ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext sc) {
		this.servletContext = sc;
	}

	public String errorToJson(List<ObjectError> errorList) {
		if (!CollectionUtils.isEmpty(errorList)) {
			Map<String, String> map = new HashMap<String, String>();
			for (ObjectError e : errorList) {
				FieldError error = (FieldError) e;
				error.getObjectName();
				error.getDefaultMessage();
				String field = error.getField();
				String message = error.getDefaultMessage();
				String rejectValue = error.getRejectedValue() + "";
				map.put(field, message);
			}
			String json = JSONObject.toJSONString(map);
			return json;
		}
		return "";
	}
	
	public String getDefaultSiteid() {
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN"))
				continue;
			if (auth.contains("MANAGE_CHECK_")) {
				String siteId = auth.substring(13, 19);
				if (!remainList.contains(siteId)) {
					remainList.add(siteId);
				}
			}
		}
		if (!CollectionUtils.isEmpty(remainList)) {
			return remainList.get(0);
		}
		return null;
	}
	
	public List<String> getSiteList() {
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN"))
				continue;
			if (auth.contains("MANAGE_CHECK_")) {
				String siteId = auth.substring(13, 19);
				if (!remainList.contains(siteId)) {
					remainList.add(siteId);
				}
			}
		}
		return remainList;
	}
	
	
	public boolean isSiteId(String siteId) {
		if (StringUtils.isBlank(siteId))
			return false;
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN"))
				continue;
			if (auth.contains("MANAGE_CHECK_")) {
				String s = auth.substring(13, 19);
				if (!remainList.contains(s)) {
					remainList.add(s);
				}
			}
		}
		if (!CollectionUtils.isEmpty(remainList)) {
			if (remainList.contains(siteId)) {
				return true;
			}
		}
		return false;
	}
}
